package cn.school.servlet;

import cn.school.entity.Grade;
import cn.school.service.GradeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet( "/subjectShowServlet")
public class SubjectShowServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Grade> all = GradeService.INST.findALL();
        response.setContentType("application/json;charset=utf-8");
        //json  []  {}  "" ,:
        StringBuilder sb=new StringBuilder();
        sb.append("[");
        for(int i=0;i<all.size();i++){
            Grade g=all.get(i);
            sb.append("{");
            sb.append("\"id\":"+g.getId());
            sb.append(",");
            sb.append("\"gname\":\""+g.getGname()+"\"");
            sb.append("}");
            if(i<all.size()-1){
                sb.append(",");
            }
        }
        sb.append("]");//gson格式的数据
        System.out.println(sb);
        response.getWriter().print(sb);
    }
}
